function ALA = gs_velPlot(GSS)
%gs_velPlot - ancillary function for velocity plot 
%
%   ALA = gs_velPlot(GSS)

Options = GridStrainOptions;

fsz = 14;       % font size 
fszr = 10;      % reduced font size
LW = 1.5;

ns = GSS.StationID;
statName = GSS.StationName; 
e = GSS.East;
n = GSS.North;
ve = GSS.EastVelocity;
vn = GSS.NorthVelocity;
a = GSS.ErrorEllipsea;
b = GSS.ErrorEllipseb;
theta = GSS.ErrorEllipsetheta;
npe = GSS.IncludedPoints;

lv = numel(e);

scalef = 0.2*max(max(e)-min(e),max(n)-min(n))/max(max(abs(ve),abs(vn)));

% for the "arrow scale" visualization: 
esca = e;  
nsca = n;
vesca = ve; 
vnsca = vn;

std_esca = std(esca-mean(esca));
std_nsca = std(nsca-mean(nsca));
esca(lv+1)  = min(esca)-std_esca/2;
nsca(lv+1)  = min(nsca)-std_nsca/2;
vesca(lv+1) = (max(abs(ve))+max(abs(vn)))*0.75;
valnum = vesca(lv+1);
valnapp = ApprInt(valnum);
val = num2str(1000*valnapp);
vesca(end) = valnapp;  % in this way, an optimal vector is drawn 
vnsca(lv+1) = 0.00;
npesca = logical([npe; 1]);

% visualization:
figki = figure;
set(figki,'Units','normalized','OuterPosition',[0 0 1 1]); 
hold on; 
plot(e(npe),n(npe),'xk','LineWidth',LW);
plot(e(~npe),n(~npe),'xm','LineWidth',LW);
quiver(esca(npesca),nsca(npesca),...
    vesca(npesca)*scalef,vnsca(npesca)*scalef,0,...
    'b','LineWidth',LW); % vector visualization 
quiver(esca(~npesca),nsca(~npesca),...
    vesca(~npesca)*scalef,vnsca(~npesca)*scalef,0,...
    'b','LineWidth',LW,'LineStyle',':'); % vector visualization 

for k = 1:lv
    [xell,yell] = ellipse(2*a(k),2*b(k),...
        e(k)+vesca(k)*scalef,n(k)+vnsca(k)*scalef,theta(k),100);
    if npe(k)
        plot(xell,yell,'r');
    else
        plot(xell,yell,'r','LineStyle',':');
    end
    if Options.VisualizePointIDs
        se = sign(vesca(k)); 
        sn = sign(vnsca(k));
        if isempty(statName)
            strStatk = num2str(ns(k));
        else
            strStatk = statName{k};
        end
        ht = text(e(k)-se*std_nsca/15,n(k)-sn*std_nsca/15,strStatk);
        if npe(k)
            set(ht,'Color','k','FontSize',fszr);
        else
            set(ht,'Color','m','FontSize',fszr);
        end
    end
end
htg = text(esca(end),nsca(end)+std_nsca/10,[val ' mm/y']);
legend('Included points','Not included points');
set(htg,'FontSize',fszr);
title('VELOCITIES AND ERROR ELLIPSES','FontSize',fsz);
set(gca,'FontSize',fszr);
xlabel('EAST (m)','FontSize',fsz); 
ylabel('NORTH (m)','FontSize',fsz);
axis equal;
ALA = axis; 